/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.games.esmi.ieat.indicatorvalues.data;

import com.vaadin.addon.timeline.Timeline;
import com.vaadin.data.Container.Indexed;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author plebani
 */
public class IndicatorSeries {
    

    
    private String name;
    private Indexed values;
    private Indexed normalized;
    private String unit;
    
    private Float average;
    private Float max;
    private Float min;
    private Float current;

    private BigDecimal nrl;
    private BigDecimal nrh;
    private BigDecimal erl;
    private BigDecimal erh;
    
    private int status;

    public IndicatorSeries(String name, Indexed values, String unit) {
        this.name = name;        
        this.values = values;
        this.unit = unit;
    }

    public String getName() {
        return name;
    }

    public Indexed getValues() {
        return values;
    }

    public String getUnit() {
        return unit;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValues(Indexed values) {
                
        this.values = values;
        
        values.addContainerProperty(Timeline.PropertyId.VALUE, Float.class,new Float(0));
	values.addContainerProperty(Timeline.PropertyId.TIMESTAMP, java.util.Date.class, null);
                
        Float sum = new Float(0);
        Float avg = new Float(0);
        Float max = new Float(Float.MIN_VALUE);
        Float min = new Float(Float.MAX_VALUE);
        Float current = null;
        Date currentdate = null;
        Date date = null;
        
        
        
        for (int i = 0; i< values.size(); i++) {
            
            
            Item item = values.getItem(values.getIdByIndex(i));
            Float value = (Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue();
            
            if (value > max) {
                max = value;
            }
            
            if (value < min) {
                min = value;
            }

            sum = sum + value;
            
            if (current == null) {
                
                current = (Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue();
                currentdate = (Date)item.getItemProperty(Timeline.PropertyId.TIMESTAMP).getValue();
            } else {
                
                date = (Date)item.getItemProperty(Timeline.PropertyId.TIMESTAMP).getValue();
            
                if (date.after(currentdate)) {
                    currentdate = date;
                    current = (Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue();
                }
                
            }

            
        }
        
        avg = sum / values.size();

        //System.out.println(current + " at " + currentdate);
        
        this.average = avg;
        this.max = max;
        this.min = min;
        this.current = current;
               
        this.normalized = this.calculateNormalized();
    }
    
    
    public Indexed calculateNormalized() {
        
        
        IndexedContainer normalizedValues = new IndexedContainer();
        normalizedValues.addContainerProperty(Timeline.PropertyId.VALUE, Float.class, new Float(0));
        normalizedValues.addContainerProperty(Timeline.PropertyId.TIMESTAMP, java.util.Date.class, null);

        
         for (int i = 0; i< values.size(); i++) {
            
            
            Item item = values.getItem(values.getIdByIndex(i));
            Float value = (Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue();
            
            Object key = normalizedValues.addItem();
            normalizedValues.getItem(key).getItemProperty(Timeline.PropertyId.TIMESTAMP).setValue((Date)item.getItemProperty(Timeline.PropertyId.TIMESTAMP).getValue());
            normalizedValues.getItem(key).getItemProperty(Timeline.PropertyId.VALUE).setValue((value-this.min)/(this.max-this.min));
         }
         
         return normalizedValues;
         
    }

    public Indexed getNormalized() {
        return normalized;
    }

    
    
    public void setUnit(String unit) {
        this.unit = unit;
    }

    public Float getAverage() {
        return average;
    }

    public Float getCurrent() {
        return current;
    }

    public Float getMax() {
        return max;
    }

    public Float getMin() {
        return min;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public BigDecimal getErh() {
        return erh;
    }

    public void setErh(BigDecimal erh) {
        this.erh = erh;
    }

    public BigDecimal getErl() {
        return erl;
    }

    public void setErl(BigDecimal erl) {
        this.erl = erl;
    }

    public BigDecimal getNrh() {
        return nrh;
    }

    public void setNrh(BigDecimal nrh) {
        this.nrh = nrh;
    }

    public BigDecimal getNrl() {
        return nrl;
    }

    public void setNrl(BigDecimal nrl) {
        this.nrl = nrl;
    }


    
    
    
    public String toString() {
        
        StringBuffer s = new StringBuffer();
        
        
        for (int i = 0; i< values.size(); i++) {
            
            
            Item item = values.getItem(values.getIdByIndex(i));
            Float value = (Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue();
            

            s.append((Date)item.getItemProperty(Timeline.PropertyId.TIMESTAMP).getValue());
            s.append(" - ");
            s.append((Float)item.getItemProperty(Timeline.PropertyId.VALUE).getValue());
            s.append("\n");
         }
         
        
        return s.toString();
        
        
    }
    
    
}
